package com.nd.microgame.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import #_package_name_#.R;

public class CustProgressBar extends LinearLayout {

    private Context mContext;

    private TextView mTip;

    private TextView mProgressTip;

    private ImageView mProgress;

    private  View mNormal;

    private int mWidth = 0;

    public  CustProgressBar(Context context,AttributeSet attributeSet) {
        super(context,attributeSet);
        this.mContext = context;
        this.initView();
    }


    private void initView() {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        View view = LayoutInflater.from(this.mContext).inflate(R.layout.cust_progress_bar, null);//也可以从XML中加载布局
        view.setLayoutParams(lp);//设置布局参数

        this.addView(view);

        mTip = this.findViewById(R.id.tip);
        mProgress = this.findViewById(R.id.activity);
        mNormal = this.findViewById(R.id.normal);

        mProgressTip = this.findViewById(R.id.progresstip);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed,l,t,r,b);
        this.mWidth =  mNormal.getWidth();
    }

    private void setLabel(String txt) {
        mTip.setText(txt);
    }

    private void setValue(int p) {
        int px = (int)Math.floor((this.mWidth * p) / 100);
        this.mProgressTip.setText(String.format("%d %%",p));

        //设置width
        ViewGroup.LayoutParams layoutParams = mProgress.getLayoutParams();
        layoutParams.width = px;
        mProgress.setLayoutParams(layoutParams);
    }

    public void setProgress(String txt,int p) {
      this.setLabel(txt);
      this.setValue(p);
    }

    public void setProgress(String txt) {
        this.setLabel(txt);
    }




}
